package com.qiantu.phone.ui.shotview;

import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.BLUE;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.COLOR_VALUE;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.GREEN;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.KELVIN;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.LIGHT;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER_OFF;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER_ON;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.RED;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.qiantu.api.entity.ControlDataColorTemperatureBean;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.ui.adapter.ColorTemperatureAdapter;
import com.qiantu.phone.widget.ColorDevicePalette;
import com.qiantu.phone.widget.ColorDeviceSeekBar;
import com.qiantu.phone.widget.ColorTemperatureSeekBar;
import com.qiantu.phone.widget.ColorWhiteSeekBar;

import org.json.JSONException;
import org.json.JSONObject;

public class ColorLightShotView extends BaseDeviceActView{
    private ColorDevicePalette devicePalette;
    private RecyclerView colorRecyclerView;
    private ColorTemperatureAdapter mColorTemperatureAdapter;
    private ColorWhiteSeekBar colorTemperatureSeekBar;
    private ImageView btnPower;
    private Boolean isOn;
    private ControlDataColorTemperatureBean mColorTemperatureInfo;
    private ControlDataColorTemperatureBean.ControlData controlData;

    public ColorLightShotView(Context context) {
        super(context);
    }

    public ColorLightShotView(Context context, String deviceType) {
        super(context, deviceType);
    }

    @Override
    public int getLayoutId() {
        return R.layout.color_light_shot_view;
    }

    @Override
    public void initView() {
        devicePalette = findViewById(R.id.round_seek_bar);
        colorRecyclerView = findViewById(R.id.color_recycler_view);
        colorTemperatureSeekBar = findViewById(R.id.color_temperature_seek_bar);
        btnPower = findViewById(R.id.btn_power);
        btnPower.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isOn = !isOn;
                if(isOn){
                    addAct(POWER, POWER_ON);
                    if(colorTemperatureSeekBar.getColorValue() != 0){
                        addAct(LIGHT, colorTemperatureSeekBar.getColorValue() + "");
                    }else{
                        //当开时，没有亮度时删除亮度动作指令
                        removeAct(LIGHT);
                        removeAct(RED);
                        removeAct(GREEN);
                        removeAct(BLUE);
                    }

                }else{
                    addAct(POWER, POWER_OFF);
                    //当关闭时，删除亮度动作指令
                    removeAct(LIGHT);
                    removeAct(RED);
                    removeAct(GREEN);
                    removeAct(BLUE);
                }
                setStatusBySwitch(isOn);
            }
        });
        devicePalette.setOnPaletteChangeListener(new ColorDevicePalette.OnPaletteChangeListener() {
            @Override
            public void onValueChanged(int colorValue) {}
            @Override
            public void onStartTrackingTouch() { }

            @Override
            public void onStopTrackingTouch(int colorValue) {
                int red = ( colorValue & 0xff0000 ) >> 16;
                int green = ( colorValue & 0xff00 ) >> 8;
                int blue = colorValue & 0xff;
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("red",red + "");
                    jsonObject.put("green",green+"");
                    jsonObject.put("blue",blue+"");
                    addAct(COLOR_VALUE,jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        colorTemperatureSeekBar.setOnSeekBarChangeListener(new ColorTemperatureSeekBar.OnSeekBarChangeListener() {
            @Override
            public void onValueChanged(int colorTemp) {}
            @Override
            public void onStartTrackingTouch() { }

            @Override
            public void onStopTrackingTouch(int colorTemp) {
                if(colorTemp == 0){
                    addAct(POWER, POWER_OFF);
                    setStatusBySwitch(false);
                    //没有亮度时删除亮度动作指令
                    removeAct(LIGHT);
                    //当关闭时，删除色温动作指令
                    removeAct(KELVIN);
                    removeAct(RED);
                    removeAct(GREEN);
                    removeAct(BLUE);
                }else{
                    addAct(POWER, POWER_ON);
                    addAct(LIGHT, colorTemp + "");
                    colorTemperatureSeekBar.setOpenState(true);
                }
            }
        });
        mColorTemperatureInfo = DeviceInfoParseUtil.parseData(getContext(), deviceType, ControlDataColorTemperatureBean.class);
        controlData = mColorTemperatureInfo.getControlData();
        if(controlData.getColorData() != null && controlData.getColorData().size() > 0){
            mColorTemperatureAdapter = new ColorTemperatureAdapter(getContext());
            mColorTemperatureAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                        mColorTemperatureAdapter.selectItem(position);
                        ControlDataColorTemperatureBean.ColorData colorData = mColorTemperatureAdapter.getData().get(position);
                        int colorValue = colorData.getColor();
                        int red = (colorValue & 0xff0000 ) >> 16;
                        int green = ( colorValue & 0xff00 ) >> 8;
                        int blue = colorValue & 0xff;
                        try {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("red",red + "");
                            jsonObject.put("green",green+"");
                            jsonObject.put("blue",blue+"");
                            addAct(COLOR_VALUE,jsonObject.toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                }
            });
            colorRecyclerView.setAdapter(mColorTemperatureAdapter);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), controlData.getColorData().size());
            colorRecyclerView.setLayoutManager(gridLayoutManager);
            mColorTemperatureAdapter.setData(controlData.getColorData());
        }
    }

    private void setStatusBySwitch(boolean isOpen){
        colorTemperatureSeekBar.setOpenState(isOpen);
        devicePalette.setOpenState(isOpen);
        btnPower.setImageResource(isOpen ? R.mipmap.icon_devicecontrol_curtain_switch_open : R.mipmap.icon_devicecontrol_curtain_switch_close);
    }

    @Override
    public void updateView() {
        //默认空的话，添加默认动作开
        if(mDeviceActMap.size() == 0){
            addAct(POWER, POWER_ON);
        }



        isOn = POWER_ON.equals(getState(POWER, "0"));
        colorTemperatureSeekBar.setColorValue(Integer.valueOf(getState(LIGHT, "0")));
        setStatusBySwitch(isOn);
    }
}
